# how the lambdas change as we vary time
# e.g.
# lambdas_time('paiva', '0')

lambdas_time <- function(filename, state)
{
	X <- read(filename)
	jump <- 10  # use this as interval
	dy <- 365*jump
	years <- c()
	lambdas <- c()
	for(year in seq(dy, length(X), 1*365)) {
		lambda <- find_lambda(Xstate(X[(year-dy):year], state), pbhp, test.distance, min)$lambda
		years <- c(years, 1946+round(year/365)+1)
		lambdas <- c(lambdas, lambda)
	}

	lambda_ <- find_lambda(Xstate(X, state), pbhp, test.distance, min)$lambda
	df1 <- data.frame(years=years,lambdas=lambdas)
	df2 <- data.frame(years=c(years[1],years[length(years)]),lambdas=c(lambda_,lambda_))

	ggplot() + geom_line(aes(x=years, y=lambdas), df2, colour='red', size=0.35) + geom_line(aes(x=years, y=lambdas), df1, size=0.8) + xlab("year") + ylab(expression(lambda^'*'))
}

# more generic version (used for sunspots)

lambdas_time2 <- function(X)
{
	jump <- 10  # use this as interval
	dy <- 133
	years <- c()
	lambdas <- c()
	for(year in seq(dy, length(X), 133)) {
		lambda <- find_lambda(X[(year-dy):year], pbhp, test.distance, min)$lambda
		years <- c(years, 1749+round(year/12)+1)
		lambdas <- c(lambdas, lambda)
	}

	lambda_ <- find_lambda(X, pbhp, test.distance, min)$lambda
	df1 <- data.frame(years=years,lambdas=lambdas)
	df2 <- data.frame(years=c(years[1],years[length(years)]),lambdas=c(lambda_,lambda_))

	ggplot() + geom_rect(aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax), data.frame(xmin=1790,xmax=1830,ymin=min(lambdas),ymax=max(lambdas)), colour='white', fill=rgb(.96,.96,.96)) + geom_line(aes(x=years, y=lambdas), df2, colour='red', size=0.35) + geom_line(aes(x=years, y=lambdas), df1, size=0.8) + xlab("year") + ylab(expression(lambda^'*'))
}

